<?php

declare (strict_types=1);
/**
 * @copyright 深圳市易果网络科技有限公司
 * @version 1.0.0
 * @link https://dayiguo.com
 */

namespace App\Service\Agent;

use App\Dao\AgentCashDao;
use App\Service\Service;

use Hyperf\Di\Annotation\Inject;

/**
 * 用户服务
 *
 *
 * @author 张昊(2396675557@qq.com)
 * @package App\Service\Admin
 */
class UserCashService extends Service
{
    /**
     * @Inject
     * @var AgentCashDao
     */
    public $dao;

    /**
     * 提现记录
     */
    public function list()
    {
        $result = $this->dao->get();

        $data = [];
        if( $result ) {
            foreach ( $result as $key => $value ) {
                $data['list'][$key]['id'] = $value['id'];
                $data['list'][$key]['amount'] = $value['amount'];
                $data['list'][$key]['status'] = $value['status'];
                $data['list'][$key]['status_text'] = $value['status_text'];
                $data['list'][$key]['created_at'] = $value['created_at'];
                $data['list'][$key]['bank_name'] = $value['bank_name'];
                $data['list'][$key]['bank_account'] = $value['bank_account'];
                $data['list'][$key]['order_no'] = $value['order_no'];
                $data['list'][$key]['start_time'] = $value['start_time'];
            }
        }
        $data['total_amount'] = $this->dao->sum([
            'agent_id' => agentId(),
        ]);
        $data['count'] = $this->dao->count([
            'agent_id' => agentId(),
        ]);
        return $data;
    }

    /**
     * 获得提现搜索
     * @param array $params
     * @return mixed
     */
    public function getCashList(array $params)
    {
        $map = [];

        $map = $this->map($map, $params);

        $sort = 'created_at desc';
        $select = ['amount','status','created_at','id','bank_name','bank_account','order_no','start_time'];
        $map['agent_id'] = agentId();
        $page_size = 10;
        $cash_list = $this->dao->getUserCashByMap($map, $sort, $select, $page_size)->toArray();

        $data['total_amount'] = $this->dao->sum([
            'agent_id' => agentId(),
        ]);
        $data['count'] = $this->dao->count([
            'agent_id' => agentId(),
        ]);
        $cash_list = array_merge($data, $cash_list);
        return $cash_list;

    }



    /**
     * 赋值查询条件
     * @param array $map
     * @param array $params
     * @return array
     */
    public function map(array $map, array $params)
    {
        switch ($params['select']??1) {
            case 1: // 时间范围
                isset($params['start_time']) && !isset($params['end_time'])  && !empty($params['start_time']) && $map['created_at'] = ['>=', strtotime($params['start_time'])]; // 提现开始时间
                isset($params['end_time']) &&  !isset($params['start_time'])  && !empty($params['end_time']) && $map['created_at'] = ['<=', strtotime($params['end_time'])]; // 提现结束时间

                isset($params['end_time']) &&  isset($params['start_time']) &&
                !empty($params['end_time']) && !empty($params['start_time']) &&
                $map['between_field'] = ['created_at' =>[strtotime($params['start_time']), strtotime($params['end_time'])]]; // 有提现开始和结束时间
                break;
            case 2:
                if ($params['now_date'] == 1) { // 本日
                    // 本日开始
                    $first = date('Y-m-d 00:00:00', time());
                    $first = strtotime($first);
                    // 本日结束
                    $last = date('Y-m-d 23:59:59', time());
                    $last = strtotime($last);
                } elseif ($params['now_date'] == 2) { // 本月
                    // 本月第一天
                    $first = date('Y-m-01 00:00:00', time());
                    // 本月最后一天
                    $last = date('Y-m-d 23:59:59', strtotime("$first +1 month -1 day"));
                    $last = strtotime($last);
                    $first = strtotime($first);

                } else { // 本年
                    // 本年第一天
                    $first = date('Y-01-01 0:0:0', time());
                    $first = strtotime($first);
                    // 本年最后一天
                    $last = date('Y-12-31 23:59:59', time());
                    $last = strtotime($last);
                }
                $map['between_field'] = ['created_at' => [$first, $last]];

                break;
            case 3:
                isset($params['status']) && $map['status'] = (int)$params['status']; //
                break;
            default:
                $this->error("未选择查询");
                break;
        }

        return $map;
    }


    
    
}